<?php

namespace App\Http\Middleware;

use Closure;

class CheckUserLogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $user_id   = intval($request->get('user_id'));
        $openid    = trim($request->get('openid'));
        $signature = trim($request->get('signature'));

        if(empty($user_id) || empty($openid) || empty($signature))
        {
            return response()->json(['code' => 401,'message' => '签名失败','status' => 200,'exception' => '','data' => (object)[]]);
        }

        if(md5($user_id.$openid.env('APP_KEY')) !== $signature)
        {
            return response()->json(['code' => 401,'message' => '签名失败','status' => 200,'exception' => '','data' => (object)[]]);
        }
        return $next($request);
    }
}
